//
//  KSHomeViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/19.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 工作台
class KSHomeViewController: KSBaseViewController {
    
    /// 左
    fileprivate lazy var leftBtn: UIButton = {
        let leftBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        leftBtn.setBackgroundImage(UIImage.init(named: "Common_Scan"), for: .normal)
        leftBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
        leftBtn.addTarget(self, action: #selector(KSHomeViewController.scanClick), for: UIControlEvents.touchUpInside)
        leftBtn.titleLabel?.font = KSMacro.FontBySize(size: 15)
        return leftBtn
    }()
    
    /// 右
    fileprivate lazy var rightBtn: UIButton = {
        let rightBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        rightBtn.setBackgroundImage(UIImage.init(named: "Common_Setting"), for: .normal)
        rightBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
        rightBtn.addTarget(self, action: #selector(KSHomeViewController.accountSettingClick), for: UIControlEvents.touchUpInside)
        rightBtn.titleLabel?.font = KSMacro.FontBySize(size: 15)
        rightBtn.titleLabel?.textAlignment = NSTextAlignment.right
        return rightBtn
    }()
    
    fileprivate lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: self.view.bounds)
        scrollView.backgroundColor = UIColor.colorWithHexString(hex: "f1eff2")
        return scrollView
    }()
    
    /// 供货管理title
    fileprivate lazy var supplyMarketTitleLabel: UILabel = {
        let supplyMarketTitleLabel = UILabel()
        supplyMarketTitleLabel.text = "供货管理"
        supplyMarketTitleLabel.font = UIFont.systemFont(ofSize: 16)
        supplyMarketTitleLabel.textColor = KSColor.kColor_3
        return supplyMarketTitleLabel
    }()
    
    /// 采购管理title
    fileprivate lazy var purchaseTitleLabel: UILabel = {
        let purchaseTitleLabel = UILabel()
        purchaseTitleLabel.text = "采购管理"
        purchaseTitleLabel.font = UIFont.systemFont(ofSize: 16)
        purchaseTitleLabel.textColor = KSColor.kColor_3
        return purchaseTitleLabel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.title = "吉拍档"
        
        self.initView()
    }
    
    fileprivate func initView() {
//        let leftButtonItem = UIBarButtonItem(customView: leftBtn)
//        self.navigationItem.leftBarButtonItem = leftButtonItem
        
        let rightButtonItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightButtonItem
        
        self.view.addSubview(self.scrollView)
        
        // 每个项目Height width 间隔
        let itemWidth = KSConstants.kScreenWidth/4
        let itemHeight:CGFloat = 60
        let div:CGFloat = 20
        
        // 采购管理
        let purchaseDescription:[String] = ["供货市场", " 搜索 ", "验收差异", "供应商管理", "采购退货"]
        let purchaseImage:[String] = ["Home_Purchase", "Home_Search", "Home_CheckDiff", "Home_Supplier", "Home_ReturnGoods"]
        var purchaseHeight:CGFloat = itemHeight + div*2
        if purchaseDescription.count > 4 {
            purchaseHeight = itemHeight * 2 +  div*3
        }
        
        if KSUserDefaultsTools.getUserType() == 1 {
            // 供货管理
            self.supplyMarketTitleLabel.frame = CGRect(x: 10, y: 10, width: 100, height: 30)
            self.scrollView.addSubview(self.supplyMarketTitleLabel)
            
            // 供货管理背景
            let supplyDescription:[String] = ["供货单管理", " 拣货 ", " 分拣 ", " 装车 "]
            let supplyImage:[String] = ["Home_SupplyMarket", "Home_Pick", "Home_Sort", "Home_Load"]
            
            
            var supplyHeight:CGFloat = itemHeight + div*2
            if supplyDescription.count > 4 {
                supplyHeight = itemHeight * 2 +  div*3
            }
            
            let supplyView = UIView(frame: CGRect(x: 0, y: self.supplyMarketTitleLabel.frame.origin.y + self.supplyMarketTitleLabel.frame.size.height, width: KSConstants.kScreenWidth, height: supplyHeight - 10))
            supplyView.backgroundColor = UIColor.white
            self.scrollView.addSubview(supplyView)
            
            for i in 0 ..< supplyDescription.count {
                let itemBtn = UIButton(frame: CGRect(x: CGFloat(i) * itemWidth, y:div, width: itemWidth, height: itemHeight))
                if i > 3 {
                    let second = i - 3
                    itemBtn.frame = CGRect(x: CGFloat(second - 1) * itemWidth, y: div*2 + itemHeight, width: itemWidth, height: itemHeight)
                }
                itemBtn.tag = 100 + i
                itemBtn.setImage(UIImage(named: supplyImage[i]), for: UIControlState.normal)
                itemBtn.setTitle(supplyDescription[i], for: UIControlState.normal)
                itemBtn.setTitleColor(KSColor.kColor_3, for: UIControlState.normal)
                itemBtn.titleLabel?.textAlignment = .center
                itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
                itemBtn.setIconInTopWithSpacing(spacing: 10)
                itemBtn.addTarget(self, action:#selector(KSHomeViewController.itemBtnClick(sender:)), for: UIControlEvents.touchUpInside)
                
                supplyView.addSubview(itemBtn)
            }
            
            self.purchaseTitleLabel.frame = CGRect(x: 10, y: supplyView.frame.origin.y + supplyView.frame.size.height + 10, width: 100, height: 30)
            self.scrollView.addSubview(self.purchaseTitleLabel)
            
            // 采购管理背景
            let purchaseView = UIView(frame: CGRect(x: 0, y: self.purchaseTitleLabel.frame.origin.y + self.purchaseTitleLabel.frame.size.height, width: KSConstants.kScreenWidth, height: purchaseHeight - 10))
            purchaseView.backgroundColor = UIColor.white
            self.scrollView.addSubview(purchaseView)
            
            for i in 0 ..< purchaseDescription.count {
                let itemBtn = UIButton(frame: CGRect(x:  CGFloat(i) * itemWidth, y:div, width: itemWidth, height: itemHeight))
                if i > 3 {
                    let second = i - 3
                    itemBtn.frame = CGRect(x: CGFloat(second - 1) * itemWidth, y: div*2 + itemHeight, width: itemWidth, height: itemHeight)
                }
                
                itemBtn.tag = 150 + i
                itemBtn.setImage(UIImage(named: purchaseImage[i]), for: UIControlState.normal)
                itemBtn.setTitle(purchaseDescription[i], for: UIControlState.normal)
                itemBtn.setIconInTopWithSpacing(spacing: 10)
                
                itemBtn.setTitleColor(KSColor.kColor_3, for: UIControlState.normal)
                itemBtn.titleLabel?.textAlignment = .center
                itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
                itemBtn.addTarget(self, action:#selector(KSHomeViewController.itemBtnClick(sender:)), for: UIControlEvents.touchUpInside)
                purchaseView.addSubview(itemBtn)
            }
        } else {
            self.purchaseTitleLabel.frame = CGRect(x: 10, y: 10, width: 100, height: 30)
            self.scrollView.addSubview(self.purchaseTitleLabel)
            
            // 采购管理背景
            let purchaseView = UIView(frame: CGRect(x: 0, y: self.purchaseTitleLabel.frame.origin.y + self.purchaseTitleLabel.frame.size.height, width: KSConstants.kScreenWidth, height: purchaseHeight - 10))
            purchaseView.backgroundColor = UIColor.white
            self.scrollView.addSubview(purchaseView)
            
            for i in 0 ..< purchaseDescription.count {
                let itemBtn = UIButton(frame: CGRect(x:  CGFloat(i) * itemWidth, y:div, width: itemWidth, height: itemHeight))
                if i > 3 {
                    let second = i - 3
                    itemBtn.frame = CGRect(x: CGFloat(second - 1) * itemWidth, y: div*2 + itemHeight, width: itemWidth, height: itemHeight)
                }
                
                itemBtn.tag = 150 + i
                itemBtn.setImage(UIImage(named: purchaseImage[i]), for: UIControlState.normal)
                itemBtn.setTitle(purchaseDescription[i], for: UIControlState.normal)
                itemBtn.setIconInTopWithSpacing(spacing: 10)
                
                itemBtn.setTitleColor(KSColor.kColor_3, for: UIControlState.normal)
                itemBtn.titleLabel?.textAlignment = .center
                itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
                itemBtn.addTarget(self, action:#selector(KSHomeViewController.itemBtnClick(sender:)), for: UIControlEvents.touchUpInside)
                purchaseView.addSubview(itemBtn)
            }
        }
    }
    
    /// 扫描
    @objc private func scanClick() {
        
    }
    
    /// 账户
    @objc private func accountSettingClick() {
        self.navigationController?.pushViewController(KSAccountSettingViewController(), animated: true)
    }
    
    /// 项目选择
    @objc private func itemBtnClick(sender: UIButton) {
        switch sender.tag {
        case 100:
            self.navigationController?.pushViewController(KSSupplyOrderViewController(), animated: true)
        case 101:
            self.navigationController?.pushViewController(KSPickingGoodsViewController(), animated: true)
        case 102:
            self.navigationController?.pushViewController(KSSortGoodsViewController(), animated: true)
        case 103:
            self.navigationController?.pushViewController(KSCarLoadingViewController(), animated: true)
        case 104:
            self.navigationController?.pushViewController(KSChargebackViewController(), animated: true)
        case 150:
            let webVC = KSWebViewController()
            webVC.url = "https://www.baidu.com"
            self.navigationController?.pushViewController(webVC, animated: true)
        case 151:
            let webVC = KSWebViewController()
            webVC.url = "https://www.baidu.com"
            self.navigationController?.pushViewController(webVC, animated: true)
        case 152:
            self.navigationController?.pushViewController(KSInboundDiffViewController(), animated: true)
        case 153:
            self.navigationController?.pushViewController(KSSupplierManageViewController(), animated: true)
        case 154:
            self.navigationController?.pushViewController(KSReturnedPurchaseViewController(), animated: true)
        default:
            break
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

